# -*- coding: utf-8 -*-
from odoo import http

# -*- coding: utf-8 -*-
import io

from odoo import http
from odoo.http import request, content_disposition
import urllib
from xlwt import *
import json

title_font = Font()
title_font.name = 'Arial Unicode MS'
title_font.bold = True
title_font.height = 360

head_font = Font()
head_font.bold = False
head_font.name = 'Arial Unicode MS'
head_font.height = 300

head_font_1 = Font()
head_font_1.bold = False
head_font_1.name = 'Arial Unicode MS'
head_font_1.height = 200


# head_style_1 = XFStyle()
# head_style_1.font = head_font_1
# head_style_1.heights=
# # head_style_1.alignment = alignment


common_font = Font()
common_font.bold = False
common_font.name = 'Arial Unicode MS'
common_font.height = 200

bold_font = Font()
bold_font.bold = True
bold_font.height = 230

alignment = Alignment()
alignment.horz = Alignment.HORZ_CENTER
alignment.vert = Alignment.VERT_CENTER

alignment1 = Alignment()
alignment1.horz = Alignment.HORZ_LEFT
alignment1.vert = Alignment.VERT_CENTER
# alignment.wrap = Alignment.HORZ_LEFT

borders = Borders()
borders.left = 1
borders.right = 1
borders.top = 1
borders.bottom = 1
borders.bottom_colour = 0x3A

common_style = XFStyle()
common_style.alignment = alignment
common_style.borders = borders

common_style_1 = XFStyle()
common_style_1.font=common_font


bold_style = XFStyle()
bold_style.font = bold_font
bold_style.alignment = alignment

datetime_style = XFStyle()
datetime_style.num_format_str = 'YYYY-MM-DD'
datetime_style.alignment = alignment1
# datetime_style.borders = borders


datetime_style1 = XFStyle()
datetime_style1.num_format_str = 'YYYY-MM-DD'
datetime_style1.alignment = alignment1
datetime_style1.borders = borders

title_style = XFStyle()
title_style.font = title_font
title_style.alignment = alignment

head_style = XFStyle()
head_style.font = head_font
head_style.alignment = alignment
# head_style.borders = borders

style = easyxf(
    'font: height 200;'
    'alignment: vert center, horizontal left;'
    'borders: left NO_LINE, right NO_LINE, top thin, bottom thin;'
)
style_top = easyxf(
    'font: height 200,name Arial Unicode MS;'
    'alignment: vert center, horizontal center;'
    'borders: left NO_LINE, right NO_LINE, top THICK, bottom NO_LINE;'
)
style_bottom = easyxf(
    'font: height 250,name Arial Unicode MS,bold True;'
    'alignment: vert center, horizontal center;'
    'borders: left NO_LINE, right NO_LINE, top NO_LINE, bottom THICK;'
)

style_1 = easyxf(
    'font: height 250;'
    'alignment: vert center, horizontal center;'
    'borders: left thin, right thin, top thin, bottom thin;'
)

style_DOTTED_bottom = easyxf(
    'font: height 200;'
    'alignment: vert center, horizontal center;'
    'borders: left NO_LINE, right NO_LINE, top NO_LINE, bottom DOTTED;'
)

style_DOTTED_top = easyxf(
    'font: height 200;'
    'alignment: vert center, horizontal center;'
    'borders: left NO_LINE, right NO_LINE, top DOTTED, bottom NO_LINE;'
)

style_DOUBLE_top = easyxf(
    'font: height 200;'
    'alignment: vert center, horizontal center;'
    'borders: left NO_LINE, right NO_LINE, top DOUBLE, bottom NO_LINE;'
)
style_title = easyxf(
    'font: height 500;'
    'alignment: vert center, horizontal center;'
    'borders: left thin, right thin, top thin, bottom thin;'
)

style_line = easyxf(
    'font: height 200,name Arial Unicode MS;'
    'alignment: vert center, horizontal center;'
    'borders: left NO_LINE, right NO_LINE, top NO_LINE, bottom NO_LINE;'
)

style_line_note = easyxf(
    'font: height 200,name Arial Unicode MS;'
    'alignment: vert center, horizontal left;'
    'borders: left NO_LINE, right NO_LINE, top NO_LINE, bottom NO_LINE;'
)

class ProductCost(http.Controller):

    @http.route('/purchase/purchase_order_detail/', type='http', auth="public", csrf=False)
    def purchase_order_detail(self, values):
        values = json.loads(values)
        content = self.write_structure_excel_purchase('purchase.order', values)
        filename = '询价单'

        return request.make_response(content, headers=[
            ('Content-Type', 'application/vnd.ms-excel'),
            ('Content-Disposition', content_disposition(filename + '.xls'))
        ])

    @http.route('/rt_purchase/purchase_order_detail_formal/', type='http', auth="public", csrf=False)
    def purchase_order_detail_formal(self, values):
        values = json.loads(values)
        content = self.write_structure_excel_purchase_formal('purchase.order', values)
        filename = '采购订单'

        return request.make_response(content, headers=[
            ('Content-Type', 'application/vnd.ms-excel'),
            ('Content-Disposition', content_disposition(filename + '.xls'))
        ])


    def write_structure_excel_purchase_formal(self, model, bom_list):
        f = Workbook()  # 创建工作簿

        xls = io.BytesIO()

        '''
        创建第一个sheet:
          sheet1
        '''
        bom_id = 0
        for bom_one_id in bom_list:
            bom_id += 1
            purchase_order = request.env[model].browse(int(bom_one_id))
            tax = ''
            if purchase_order.order_line and purchase_order.order_line[0].taxes_id:
                tax = purchase_order.order_line[0].taxes_id.name

            sheet1 = f.add_sheet(u'sheet' + str(bom_id), cell_overwrite_ok=True)  # 创建sheet

            data = []
            sheet1.write_merge(0, 0, 0, 11, purchase_order.company_id.name, style=title_style)
            sheet1.write_merge(1, 1, 0, 11, u'', style=style_DOUBLE_top)
            sheet1.write_merge(2, 2, 0, 11, u'', style=head_style)
            sheet1.write_merge(3, 3, 0, 11, u'Purchase Order', style=head_style)
            sheet1.write_merge(4, 4, 0, 5, u'Order No.:'+purchase_order.name,style=common_style_1)
            sheet1.write_merge(4, 4, 6, 11, u'Order date:'+ str(purchase_order.date_order),style=common_style_1)
            sheet1.write_merge(5, 5, 0, 5, u'Supplier:' + purchase_order.partner_id.name, style=common_style_1)
            sheet1.write_merge(5, 5, 6, 11, u'Trading date:' + str(purchase_order.planned_date_new), style=common_style_1)
            sheet1.write_merge(6, 6, 0, 5, u'Contact:' + purchase_order.partner_id.contact_name, style=common_style_1)
            sheet1.write_merge(6, 6, 6, 11, u'Payment term:' + str(purchase_order.payment_term_id.name), style=common_style_1)
            sheet1.write_merge(7, 7, 0, 5, u'Tel::' + purchase_order.partner_id.phone, style=common_style_1)
            sheet1.write_merge(7, 7, 6, 9, u'Currency:' + str(purchase_order.currency_id.name), style=common_style_1)
            sheet1.write_merge(7, 7, 10, 11, u'Tax Rate:' + str(purchase_order.order_line[0].taxes_id[0].amount), style=common_style_1)

            '''
            No.	Product name	Product No.	Number	Unit	Unit Price	Amount	Note

            '''
            sheet1.write(8, 0, u'No.', style_top)
            sheet1.write_merge(8, 8, 1,3,u'Product name', style_top)
            sheet1.write_merge(8, 8, 4, 6, u'Product No', style_top)
            sheet1.write(8,7 ,u'Number', style_top)
            sheet1.write(8, 8, u'Unit', style_top)
            sheet1.write(8, 9, u'Unit Price', style_top)
            sheet1.write(8, 10, u'Amount', style_top)
            sheet1.write(8, 11, u'Note', style_top)
            current_row = 9
            seq = 1
            for line in purchase_order.order_line:
                sheet1.write(current_row, 0, seq, style_line)
                sheet1.write_merge(current_row, current_row, 1, 3, line.product_id.name, style_line)
                sheet1.write_merge(current_row, current_row, 4, 6, line.product_id.description, style_line)
                sheet1.write(current_row, 7, line.product_qty, style_line)
                sheet1.write(current_row, 8, line.product_uom.name, style_line)
                sheet1.write(current_row, 9, line.price_unit, style_line)
                sheet1.write(current_row, 10, line.price_total, style_line)
                sheet1.write(current_row, 11, line.remark if line.remark else '', style_line)
                seq=seq+1
                current_row = current_row + 1
            next=current_row+6
            sheet1.write_merge(next,next, 0,5 ,'',style_bottom)
            sheet1.write_merge(next, next, 6, 11,'Total fee :'+str("{:,}".format(purchase_order.amount_total)),style_bottom )
            sheet1.write_merge(next+7, next+7, 0, 11, '',
                               style_DOTTED_bottom)
            note_line=next+8
            if purchase_order.notes:
                for note_line_context in purchase_order.notes.split('\n'):
                    sheet1.write_merge(note_line, note_line, 0, 11, note_line_context,
                                       style_line_note)
                    note_line=note_line+1
            sheet1.write_merge(note_line, note_line, 0, 11, '',
                               style_DOTTED_top)

            sheet1.write_merge(note_line+2, note_line+2, 0, 5,'Documentation: '+purchase_order.create_uid.name,
                               style_line)

            sheet1.write_merge(note_line + 2, note_line + 2, 6, 11,'Supplier sign:',
                               style_line)

            for x, i in enumerate([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2]):
                sheet1.col(x).width = 2560 * i
            sheet1.row(0).height = 500
            order_len=len(purchase_order.order_line)

            for x in (8,8+order_len):
                sheet1.row(x).height_mismatch  =True
                sheet1.row(x).height =500

            f.save(xls)
            xls.seek(0)
            content = xls.read()

        return content

